package com.community.controller;
import com.community.dao.ElderDao;
import com.community.dao.HealthDao;
import com.community.dao.impl.ElderDaoImpl;
import com.community.dao.impl.HealthDaoImpl;
import com.community.model.Elder;
import com.community.model.HealthInfo;

import com.community.service.ElderService;
import com.community.service.HealthService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet("/UserHealthInsert")
public class ElderHealthInsertController extends HttpServlet {

    public static final Logger log =
            LoggerFactory.getLogger(ElderHealthInsertController.class);
    private HealthService healthService =new HealthService();
    private ElderService elderService=new ElderService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. 获取并验证elderId
        String elderIdParam = request.getParameter("elderId");
        if (elderIdParam == null || elderIdParam.isEmpty()) {
            log.error("缺少elderId参数");
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "请选择关联的长者");
            return;
        }

        Integer elderId;
        try {
            elderId = Integer.valueOf(elderIdParam);
        } catch (NumberFormatException e) {
            log.error("无效的elderId格式: {}", elderIdParam);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "长者ID格式错误");
            return;
        }

        // 验证长者是否存在
        Elder elder = elderService.getElderById(elderId);
        if (elder == null) {
            log.error("无效的elderId: {}", elderId);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "指定的长者不存在");
            return;
        }

        String recordDate = request.getParameter("recordDate");
        LocalDateTime createdAt;
        if (recordDate != null && !recordDate.isEmpty()) {
            createdAt = LocalDateTime.parse(recordDate);
        } else {
            createdAt = LocalDateTime.now();
        }

        //温度
        double temperature = 0.0;
        String temperatureParam = request.getParameter("temperature");

        if (temperatureParam != null && !temperatureParam.isEmpty()) {
            try {
                temperature = Double.parseDouble(temperatureParam);
            } catch (NumberFormatException e) {
                // 处理无效数字格式的情况
                log.error("无效的温度参数: {}", temperatureParam, e);
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "温度必须是数字");
                return;
            }
        } else {
            // 处理参数缺失的情况
            log.warn("缺少温度参数");
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "缺少温度参数");
            return;
        }
        String bloodPressure =request.getParameter("bloodPressure");
        String symptoms = request.getParameter("symptoms");


        HealthInfo healthinfo=new HealthInfo();
        healthinfo.setElderId(elderId);
        healthinfo.setRecordDate(createdAt);
        healthinfo.setTemperature(temperature);
        healthinfo.setBloodPressure(bloodPressure);
        healthinfo.setSymptoms(symptoms);

        // 调用服务层
       healthService.submitHealthInfo(healthinfo);
        response.sendRedirect(request.getContextPath() + "/index2.jsp");
    }
}